package net.kem.android.positioner.responsehandler;

import net.kem.android.positioner.Macro;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import java.util.ArrayList;

/**
<message>
	<idRequest>10</idRequest>
	<phoneNumber>050720737</phoneNumber>
</message>

<message>
	<status></status>
	<descr></descr>
	<phoneNumber>050720737</phoneNumber>
	<idRequest>10</idRequest>
	<macros>
		<macro>
			<name></name>
			<id></id>
		</macro>
	</macros>
</message>

 * @author Zhenya
 *
 */
public class MacrosResponseHandler extends ResponseHandler {
	private ArrayList<Macro> macros;
	private Macro currentMacro;

	@Override
	public void startDocument() throws SAXException {
		macros = new ArrayList<Macro>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		super.startElement(uri, localName, qName, atts);
		if(localName.equals("macro")) {
			currentMacro = new Macro();
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(localName.equals("macro")) {
			macros.add(currentMacro);
		} else if(localName.equals("id")) {
			currentMacro.setId(Integer.parseInt(currentValue));
		} else if(localName.equals("name")) {
			currentMacro.setName(currentValue);
		} else {
			super.endElement(uri, localName, qName);
		}
	}

	public ArrayList<Macro> getMacros() {
		return macros;
	}
}